/**
 * Copyright (c) 2010, iDA MediaFoundry
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * - Neither the name of iDA MediaFoundry nor the names of its contributors may
 *   be used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package be.idamf.flash.net
{
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	/**
	 * Queues loader.load(request, context) sequentially and advances on the 
	 * next frame. This takes longer to load, but yields better performance
	 * (maintaining framerate) for a large number of loader instances.
	 * 
	 * @author Jan Van Coppenolle
	 */
	public function queueLoader(loader:Loader, request:URLRequest, context:LoaderContext = null):void
	{
		new QueuedLoader(loader, request, context);
	}
}

import be.idamf.flash.exec.deferExec;

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;

internal class QueuedLoader
{
	public static const QUEUE:Array = [];
	
	public static function load(queuedLoader:QueuedLoader):void
	{
		with (queuedLoader)
		{
			deferExec(loader, loader.load, true, request, context);
			addHandlers(loader.contentLoaderInfo);
		}
	}

	private static function loadHandler(event:Event):void
	{
		removeHandlers(LoaderInfo(event.target));
		
		QUEUE.shift();
		
		if (QUEUE.length != 0)
			load(QUEUE[0] as QueuedLoader);
	}
	
	private static function addHandlers(target:LoaderInfo):void
	{
		target.addEventListener(Event.COMPLETE, loadHandler);
		target.addEventListener(IOErrorEvent.IO_ERROR, loadHandler);
	}

	private static function removeHandlers(target:LoaderInfo):void
	{
		target.removeEventListener(Event.COMPLETE, loadHandler);
		target.removeEventListener(IOErrorEvent.IO_ERROR, loadHandler);
	}
	
	////////////////////////////////////////////////////////////////////////////
	
	public var loader:Loader;
	public var request:URLRequest;
	public var context:LoaderContext;
	
	public function QueuedLoader(loader:Loader, request:URLRequest, context:LoaderContext)
	{
		this.loader = loader;
		this.request = request;
		this.context = context;
		
		QUEUE.push(this);

		if (QUEUE.length == 1)
			load(this);
	}
}